package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.godmch.parameter.getAgentRateBean;
import com.ymatou.iapi.godmch.service.getAgentRateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*获取代理商费率
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentRate{
	private static getAgentRateBean bean;
	private static getAgentRateCall call;
	
	public agent_typeWapper agentTypeWapper = new agent_typeWapper();
	private static mch_relationWapper mchrelationWapper=new mch_relationWapper();
	
	private final static Integer channlNoAgent=500400810;//渠道无代理
	private final static Integer channlAndAgent=500400812;//渠道有代理
	private final static Integer agentId=500400813;//500400813 的上级是500400812
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取代理商费率");
	}

	@Before
	public void caseUp() {
		bean=new getAgentRateBean();
		call=new getAgentRateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRate_001() {
		Logger.start(true,"正例-获取代理商费率-正常");
		try {
			
			bean.setMchId(channlAndAgent);
			bean.setAgentId(agentId);
			call.setData(bean);
			call.callService();			
			
			//查询代理与商户关系mch_relation
	        List<mch_relation> mchrelations=mchrelationWapper.selectByPrimaryKey(agentId, channlAndAgent);
	        
			//根据代理类型id 获取代理类型
			Map<String, Object> map = new HashMap();
			map.put("agentType", mchrelations.get(0).getAgentType());
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");	
			Logger.verifyEquals(list.get(0).getRate().stripTrailingZeros().toPlainString(), new BigDecimal(call.getAgentRate()).stripTrailingZeros().toPlainString(),"验证返回的agentRate与数据库中的rate是否一致");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRate_001_1() {
		Logger.start(true,"正例-获取代理商费率-代理费率等于0");
		try {
			
			//查询代理与商户关系mch_relation
	        List<mch_relation> mchrelations=mchrelationWapper.selectByPrimaryKey(agentId, channlAndAgent);
	    	//根据代理类型id 获取代理类型
			Map<String, Object> map = new HashMap();
			map.put("agentType", mchrelations.get(0).getAgentType());
			List<agent_type> list = agentTypeWapper.selectby(map);
			//修改
			list.get(0).setRate(new BigDecimal(0));
			int result=agentTypeWapper.updateByPrimaryKey(list.get(0));

			bean.setMchId(channlAndAgent);
			bean.setAgentId(agentId);
			call.setData(bean);
			call.callService();			
	        
			//根据代理类型id 获取代理类型
			list = agentTypeWapper.selectby(map);
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");	
			Logger.verifyEquals("0", new BigDecimal(call.getAgentRate()).stripTrailingZeros().toPlainString(),"验证返回的agentRate与数据库中的rate是否一致");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRate_002() {
		Logger.start(true,"正例-获取代理商费率-商户id为空");
		try {
			//bean.setMchId(channlAndAgent);
			bean.setAgentId(agentId);
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRate_003() {
		Logger.start(true,"正例-获取代理商费率-代理id为空");
		try {
			
			bean.setMchId(channlAndAgent);
			//bean.setAgentId(agentId);
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRate_004() {
		Logger.start(true,"正例-获取代理商费率-商户id与代理id不一致");
		try {
			
			bean.setMchId(channlNoAgent);
			bean.setAgentId(agentId);
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals("{}", call.getString("result"), "验证result");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}